package com.biluo.mvs.adapter

import android.annotation.SuppressLint
import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.BaseAdapter
import android.widget.TextView
import com.biluo.mvs.R
import com.biluo.mvs.model.Ticket
import java.text.NumberFormat
import java.util.*

class TicketAdapter(private var context: Context, private var ticketList: List<Ticket>): BaseAdapter() {
    override fun getCount(): Int {
        return ticketList.size
    }

    override fun getItem(position: Int): Ticket {
        return ticketList[position]
    }

    override fun getItemId(position: Int): Long {
        return position.toLong()
    }

    @SuppressLint("SetTextI18n")
    override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
        val viewHolder: ViewHolder
        val view: View
        if (convertView == null) {
            view = LayoutInflater.from(context).inflate(R.layout.ticket_item, null)
            val idTv = view.findViewById<TextView>(R.id.id)
            val waitFineTv = view.findViewById<TextView>(R.id.waitFine)
            val waitOverdueFeeTv = view.findViewById<TextView>(R.id.waitOverdueFee)
            viewHolder = ViewHolder(idTv, waitFineTv, waitOverdueFeeTv)
            view.tag = viewHolder
        } else {
            view = convertView
            viewHolder = view.tag as ViewHolder
        }
        val ticket = getItem(position)
        viewHolder.idTv.text = ticket.id.toString()
        val format = NumberFormat.getInstance(Locale.US)
        format.minimumFractionDigits = 2
        format.maximumFractionDigits = 2
        viewHolder.waitFineTv.text = format.format(ticket.waitFine) + " 元"
        viewHolder.waitOverdueFeeTv.text = format.format(ticket.waitOverdueFee) + " 元"
        return view
    }

    class ViewHolder(var idTv: TextView, var waitFineTv: TextView, var waitOverdueFeeTv: TextView)

}